﻿using BLL;
using Inventory.BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PSIMIS2019.SubPages
{
    public partial class _Customer : PSIMIS2019.Templates.BaseInfoForm
    {
        private Customer c;
        private Contact contact;
        private CustomerBLL bl;
        private List<Customer> list;
        public _Customer()
        {
            InitializeComponent();
            bl = new CustomerBLL();
            dgvCustomer.AutoGenerateColumns = false;//使清空DataGridView控件数据源时，标题内容不被清空
            BindData();//绑定DataGridView的数据源
            ControlOpera(false);
        }
        private void BindData()
        {
            dgvCustomer.DataSource = null;//清空DataGridView的数据源
            list = bl.GetList();
            dgvCustomer.DataSource = list;//将list集合数据绑定到控件dgvCustomer作为数据源
        }
        /// <summary>
        /// 定义单击 “新增/修改/删除”按钮式，GroupBox控件中TextBox的可操作性
        /// </summary>
        /// <param name="value"></param>
        private void ControlOpera(bool value)
        {
            txtCustomerID.Enabled = value;
            txtCustomerName.Enabled = value;
            txtAddress.Enabled = value;
            txtZipCode.Enabled = value;
            txtTel.Enabled = value;
            txtFax.Enabled = value;
            txtBankName.Enabled = value;
            txtBankAccount.Enabled = value;
            txtContacter.Enabled = value;
            txtEmail.Enabled = value;
        }
        /// <summary>
        /// 新增 按钮单击事件
        /// </summary>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            ControlOpera(true);
            txtSpellingCode.Enabled = false;
        }
        /// <summary>
        /// 将业务实体内容显示到TextBox控件中
        /// </summary>
        private void ShowInfo()
        {
            txtCustomerID.Text = c.CustomerID.Trim();
            txtCustomerName.Text = c.CustomerName.Trim();
            txtSpellingCode.Text = c.ContactInfo.SpellingCode.Trim();
            txtAddress.Text = c.ContactInfo.Address.Trim();
            txtZipCode.Text = c.ContactInfo.ZipCode.Trim();
            txtTel.Text = c.ContactInfo.Tel.Trim();
            txtFax.Text = c.ContactInfo.Fax.Trim();
            txtBankName.Text = c.ContactInfo.BankName.Trim();
            txtBankAccount.Text = c.ContactInfo.BankAccount.Trim();
            txtContacter.Text = c.ContactInfo.Contacter.Trim();
            txtEmail.Text = c.ContactInfo.Email.Trim();
        }
        /// <summary>
        /// 修改 按钮单击事件
        /// </summary>
        private void btnModify_Click(object sender, EventArgs e)
        {
            ControlOpera(true);
            txtCustomerID.Enabled = false;
            txtSpellingCode.Enabled = false;
            if (c != null)
                ShowInfo();
        }
        /// <summary>
        /// 删除 按钮单击事件
        /// </summary>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (c != null)
                ShowInfo();
        }
        /// <summary>
        /// 文本框输入内容数据验证
        /// </summary>
        private string Validation()
        {
            if (txtCustomerID.Text.Trim() == "") { txtCustomerID.Focus(); return "客户编号不能为空值！"; }
            if (txtCustomerName.Text.Trim() == "") { txtCustomerName.Focus(); return "客户名称不能为空值！"; }
            if (txtAddress.Text.Trim() == "") { txtAddress.Focus(); return "地址不能为空值！"; }
            if (txtZipCode.Text.Trim() == "") { txtZipCode.Focus(); return "邮编不能为空值！"; }
            if (txtTel.Text.Trim() == "") { txtTel.Focus(); return "联系方式不能为空值！"; }
            if (txtFax.Text.Trim() == "") { txtFax.Focus(); return "传真不能为空值！"; }
            if (txtBankName.Text.Trim() == "") { txtBankName.Focus(); return "银行名称不能为空值！"; }
            if (txtBankAccount.Text.Trim() == "") { txtBankAccount.Focus(); return "银行账号不能为空值！"; }
            if (txtContacter.Text.Trim() == "") { txtContacter.Focus(); return "联系人不能为空值！"; }
            if (txtEmail.Text.Trim() == "") { txtEmail.Focus(); return "Email不能为空值！"; }
            return "";
        }
        /// <summary>
        /// 获取TextBox控件内容，并完成Customer实例化
        /// </summary>
        private Customer SetValue()
        {
            c = new Customer();
            c.CustomerID   = txtCustomerID.Text.Trim();
            c.CustomerName = txtCustomerName.Text.Trim();
            contact = new Contact();
            contact.SpellingCode = txtSpellingCode.Text.Trim();
            contact.Address      = txtAddress.Text.Trim();
            contact.ZipCode      = txtZipCode.Text.Trim();
            contact.Tel          = txtTel.Text.Trim();
            contact.Fax          = txtFax.Text.Trim();
            contact.BankName     = txtBankName.Text.Trim();
            contact.BankAccount  = txtBankAccount.Text.Trim();
            contact.Contacter    = txtContacter.Text.Trim();
            contact.Email        = txtEmail.Text.Trim();
            c = new Customer(c, contact);
            return c;
        }
        /// <summary>
        /// 存盘 按钮单击事件
        /// </summary>
        private void btnSave_Click(object sender, EventArgs e)
        {
            string errorString = Validation();
            if (errorString.Length > 0) { MessageBox.Show(errorString, "系统提示");return; }
            if (opera == Operation.INSERT)
            {
                SetValue();//获取控件值并完成对象赋值
                if (bl.Insert(c))
                {
                    BindData();//绑定DataGridView的数据源
                    Clear();//清空TextBox控件内容
                    MessageBox.Show("新增数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("新增数据失败！", "系统提示");
            }
            if (opera == Operation.UPDATE)
            {
                if (c != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Update(c))
                    {
                        BindData();//绑定DataGridView的数据源
                        ShowInfo();
                        MessageBox.Show("成功更新数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("新增数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要修改的记录", "系统提示");
            }
            if (opera == Operation.DELETE)
            {
                if (c != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Delete(c))
                    {
                        BindData();//绑定DataGridView的数据源
                        Clear();//清空TextBox控件内容
                        MessageBox.Show("成功删除数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("删除数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要删除的记录", "系统提示");
            }
        }

        private void dgvCustomer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvCustomer.CurrentRow.Selected = true;
            c = list[e.RowIndex];
            if(opera==Operation.UPDATE || opera == Operation.DELETE)
                ShowInfo();
        }
        /// <summary>
        /// 处理关联关系类的DataGridView控件显示内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvCustomer_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dgvCustomer.Columns[e.ColumnIndex].Name == "SpellingCode")
                e.Value = ((Contact)e.Value).SpellingCode;
            if (dgvCustomer.Columns[e.ColumnIndex].Name == "Address")
                e.Value = ((Contact)e.Value).Address;
            if (dgvCustomer.Columns[e.ColumnIndex].Name == "ZipCode")
                e.Value = ((Contact)e.Value).ZipCode;
            if (dgvCustomer.Columns[e.ColumnIndex].Name == "Tel")
                e.Value = ((Contact)e.Value).Tel;
            if (dgvCustomer.Columns[e.ColumnIndex].Name == "Fax")
                e.Value = ((Contact)e.Value).Fax;
            if (dgvCustomer.Columns[e.ColumnIndex].Name == "BankName")
                e.Value = ((Contact)e.Value).BankName;
            if (dgvCustomer.Columns[e.ColumnIndex].Name == "BankAccount")
                e.Value = ((Contact)e.Value).BankAccount;
            if (dgvCustomer.Columns[e.ColumnIndex].Name == "Contacter")
                e.Value = ((Contact)e.Value).Contacter;
            if (dgvCustomer.Columns[e.ColumnIndex].Name == "Email")
                e.Value = ((Contact)e.Value).Email;
        }

        private void txtCustomerName_TextChanged(object sender, EventArgs e)
        {
            txtSpellingCode.Text = PY.HZToSpell(txtCustomerName.Text.Trim());
        }
    }
}
